% Short script to identify orientation index of imageJ generated
% orientation histograms. By Paul V. Taufalele 2019.
dir_name = uigetdir();
cd(dir_name);
file_list = dir(dir_name);
[num_files] = size(file_list);
oindexes = [];
for i = 1:(num_files-2)
    file_matrix = dlmread(file_list(i+2).name, ',', 1, 0);
    file_matrix = file_matrix';
    Y = file_matrix(2, :)';
    X = [1:180];
    X = X';
    avg_angle = round(sum(X.*Y)/sum(Y));
    alpha = abs(X - avg_angle);
    idx = find(alpha > 90);
    alpha(idx) = 180-alpha(idx);
    [a_sorted idx_sort] = sort(alpha);
    alpha_norm = [0:90];  %%% This Variable will be the 'X' 
    for j = 0:90 
        temp_idx = find(alpha == j);
        alpha_counts(j+1) = sum(Y(temp_idx)); %% This variable will be N(alpha)
    end
    t_alpha_counts = alpha_counts.*cosd(alpha_norm).^2; %% This will be N(alpha)*cos^2(alpha)
    % Calculate the Orientation Index via TRAPZ integration method
    oindexes(i) = 2*(trapz(alpha_norm,t_alpha_counts)/trapz(alpha_norm, alpha_counts))-1;
end
